package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.command.Command;
import com.jp.knowledge.command.CommandInvoker;
import com.jp.knowledge.command.CommandReceiver;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.MessageSystemModel;
import com.jp.knowledge.view.JpDiver;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/10.
 */

public class MessageSystemAdapter extends RecycleVAdapter<MessageSystemModel> implements RecycleVAdapter.OnItemClickListener {

    private SimpleDateFormat itemDateFormat;
    private SimpleDateFormat contentDateFormat;

    public MessageSystemAdapter(Context context, @NonNull List<MessageSystemModel> list) {
        super(context, list);
        itemDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        contentDateFormat = new SimpleDateFormat("MM月dd日");
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_message_system;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        MessageSystemModel data = getData(position);
        holder.setTextView(R.id.message_time, itemDateFormat.format(data.getCreateTime()));
        if(!TextUtils.isEmpty(data.getHeaderImg())){
            holder.get(R.id.message_header_icon).setVisibility(View.VISIBLE);
            holder.get(R.id.message_header_color).setVisibility(View.GONE);
            holder.setImageUrl(R.id.message_header_icon, data.getHeaderImg());
        } else if(TextUtils.isEmpty(data.getHeaderColor())){
            holder.get(R.id.message_header_icon).setVisibility(View.GONE);
            holder.get(R.id.message_header_color).setVisibility(View.VISIBLE);
            try {
                holder.get(R.id.message_header_color).setBackgroundColor(Color.parseColor(data.getHeaderColor()));
            } catch (Exception e){
                holder.get(R.id.message_header_color).setBackground(null);
            }
        } else{
            holder.get(R.id.message_header_icon).setVisibility(View.GONE);
            holder.get(R.id.message_header_color).setVisibility(View.GONE);
        }
        holder.setTextView(R.id.message_content_title, data.getInfoTitle());
        holder.setTextView(R.id.message_content_time, contentDateFormat.format(data.getCreateTime()));
        int layout = data.getPushLayout();
        if(layout == 0){            // 纯文字
            holder.get(R.id.message_content_img).setVisibility(View.GONE);
            holder.get(R.id.message_content_rv).setVisibility(View.GONE);
            holder.get(R.id.message_content_tv).setVisibility(View.VISIBLE);
            holder.get(R.id.message_bottom_line).setVisibility(View.VISIBLE);
            holder.get(R.id.message_bottom_more).setVisibility(View.VISIBLE);
            holder.setTextView(R.id.message_content_tv, Html.fromHtml(data.getInfoHtml()));
        } else if(layout == 1){     // 单图文
            holder.get(R.id.message_content_img).setVisibility(View.VISIBLE);
            holder.get(R.id.message_content_rv).setVisibility(View.GONE);
            holder.get(R.id.message_content_tv).setVisibility(View.VISIBLE);
            holder.get(R.id.message_bottom_line).setVisibility(View.VISIBLE);
            holder.get(R.id.message_bottom_more).setVisibility(View.VISIBLE);
            holder.setImageUrl(R.id.message_content_img, data.getInfoPic());
            holder.setTextView(R.id.message_content_tv, Html.fromHtml(data.getInfoHtml()));
        } else /*if(layout == 2)*/{     // 多图文
            holder.get(R.id.message_content_img).setVisibility(View.VISIBLE);
            holder.get(R.id.message_content_tv).setVisibility(View.GONE);
            holder.get(R.id.message_bottom_line).setVisibility(View.GONE);
            holder.get(R.id.message_bottom_more).setVisibility(View.GONE);
            try {
                holder.setImageUrl(R.id.message_content_img, data.getList().get(0).getInfoPic());
            } catch (Exception e){
                holder.setImageUrl(R.id.message_content_img, data.getInfoPic());
            }
            RecyclerView recyclerView = holder.get(R.id.message_content_rv);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            List<MessageSystemModel.ListBean> listBeen = new ArrayList<>();
            if(data.getList() != null && data.getList().size() > 0)
                listBeen.addAll(data.getList());
            if(listBeen.size() > 0)
                listBeen.remove(0);
            recyclerView.setAdapter(new MessageSystemInfoAdapter(mContext, listBeen));
        }
        addItemClickListener(this);
    }

    @Override
    public void itemSelect(int position) {
        MessageSystemModel data = mList.get(position);
        CommandReceiver commandReceiver = new CommandReceiver.Builder(mContext)
                .withCompanyId(data.getCompanyId())
                .withRecordId(data.getRecordId())
                .build();
        CommandInvoker.executeCommand(Command.create(data.getCommand(), commandReceiver));
    }

    private class MessageSystemInfoAdapter extends RecycleVAdapter<MessageSystemModel.ListBean> implements OnItemClickListener {
        public MessageSystemInfoAdapter(Context context, @NonNull List<MessageSystemModel.ListBean> list) {
            super(context, list);
        }
        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_message_system_info;
        }
        @Override
        public void onBind(RecycleVHolder holder, int position) {
            MessageSystemModel.ListBean data = getData(position);
            holder.setTextView(R.id.message_info_title, data.getInfoTitle());
            holder.setImageUrl(R.id.message_info_icon, data.getInfoPic());
            addItemClickListener(this);
        }

        @Override
        public void itemSelect(int position) {
            MessageSystemModel.ListBean data = getData(position);
            CommandReceiver commandReceiver = new CommandReceiver.Builder(mContext)
                    .withCompanyId(data.getCompanyId())
                    .withRecordId(data.getRecordId())
                    .build();
            CommandInvoker.executeCommand(Command.create(data.getCommand(), commandReceiver));
        }
    }
}
